

package ivan.cheng.adapter;

import ivan.cheng.Const;
import ivan.cheng.R;
import ivan.cheng.activitys.AppDetailActivity;
import ivan.cheng.utils.Util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class PackageListAdapters {

    public final static class PackageListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private List<PackageInfo> list;
        private Activity ctx;
        private PackageManager pm;

        public PackageListAdapter(Activity context, List<PackageInfo> list) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            this.list = list;
            this.ctx = context;
            this.pm = ctx.getPackageManager();
        }

        public int getCount() {
            return list.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_detail_item, null);

                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.list_icon);
                holder.text_name = (TextView) convertView.findViewById(R.id.list_name);
                holder.text_count = (TextView) convertView.findViewById(R.id.permission_count);
                holder.check_detail = (ImageButton) convertView.findViewById(R.id.check_detail);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final PackageInfo dp = list.get(position);
           
            convertView.setVisibility(View.VISIBLE);
            holder.icon.setImageDrawable(dp.applicationInfo.loadIcon(pm));
            
            if(dp.applicationInfo.loadLabel(pm).toString().contains("Google"))
            	this.toString();
            holder.text_name.setText(dp.applicationInfo.loadLabel(pm).toString());
            holder.check_detail.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(ctx, AppDetailActivity.class);
					intent.putExtra(Const.EXTRA_PACKAGE_NAME, dp.packageName);
					ctx.startActivity(intent);
				}
			});
			if (null == dp.requestedPermissions)
				holder.text_count.setText(ctx.getResources().getString(
						R.string.permission_count,
						0));
			else
			{
				int w = Util.GetWarningPermissionCount(dp,pm);
				 holder.text_count.setText(Html.fromHtml(ctx.getResources().getString(
							R.string.permission_count,
							dp.requestedPermissions.length)+
							"<br><font size=\"5\" color=\"0xee0000\">"  + 
							ctx.getResources().getString(
									R.string.dangdrous_count,w)
									+"</font>"));
			}
            
            return convertView;
        }

    }

    private static class ViewHolder 
    {
        ImageView icon;
        TextView text_name;
        TextView text_count;
        ImageButton check_detail;
    }

}
